<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>

<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  data() {
    return {
      load: true,

      configuration: null, //展示的公众号信息

      form: {
        // 证书名称
        certName: "",
        // 证书路径
        keyPath: "",
        // 微信支付商户号
        mchId: "",
        // 微信支付密钥
        mchKey: "",
        // 付款类型
        paymentType: "TRANSFERS",
        wechatType: "JSAPI",
      },

      fileList: [],

      apiUrl: {
        detail: "mall-site-portal/payment-config/getItem",
        configuration: "mall-site-portal/payment-config/getWeChatMpItem",
        save: "mall-site-portal/payment-config/saveOrUpdate",
        uploadCert: "mall-site-portal/payment-config/uploadCert",
      },
    };
  },
  mounted() {},
  methods: {
    init() {
      this.initData();
      this.getData();
      this.getConfiguration();
    },

    // 文件上传
    async onChange(file) {
      console.error(file);
      let fd = new FormData();
      fd.append("certFile", file.raw);

      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.uploadCert,
          isQsData: false,
          data: fd,
        },
        window
      );

      if (_data._ok) {
        this.form.keyPath = _data.data;
        this.form.certName = file.name;
      }
    },

    // 文件删除
    onRemove() {
      this.form.keyPath = "";
      this.form.certName = "";
    },

    // 保存
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      this.load = true;
      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.save,
          data: {
            ...this.form,
          },
        },
        window
      );

      if (_data._ok) {
        this.init();
        this.$vFN.Tools.messageSuccess("保存修改成功");
      }

      this.load = false;
    },

    // 获取内容信息
    async getData() {
      this.load = true;
      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            paymentType: "TRANSFERS",
            wechatType: "JSAPI",
          },
        },
        window
      );

      if (_data._ok) {
        for (let i in this.form) {
          let _d = _data.data[i];
          if (_d != null) {
            this.form[i] = _d;
          }
        }
      }

      // 将脱敏的mchKey 赋值到表单中
      this.form.mchKey = _data.data.mchKeyDesensitized;

      this.load = false;
    },

    // 获取公众号配置
    async getConfiguration() {
      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.configuration,
        },
        window
      );

      if (_data._ok) {
        this.configuration = _data.data;
      }
    },

    // 验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false);
        });
      });
    },

    initData() {
      this.fileList = [];
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>
